import {
  BaseEntity,
  BeforeUpdate,
  Column,
  Entity,
  PrimaryGeneratedColumn,
} from "typeorm";
import { dbConfig } from "@/common/config";

@Entity()
export class Upload extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    type: "varchar",
    name: "file_name",
    length: 255,
    nullable: true,
    comment: "文件的名称（包括扩展名）",
  })
  file_name: string;

  @Column({
    type: "varchar",
    name: "file_originalname",
    length: 255,
    nullable: true,
    comment: "文件原名称 上传的名称",
  })
  file_originalname: string;

  @Column({
    type: "varchar",
    name: "file_path",
    length: 500,
    nullable: true,
    comment: "文件在服务器上的存储路径（相对于根目录或绝对路径）",
  })
  file_path: string;

  @Column({
    type: "bigint",
    name: "file_size",
    nullable: true,
    comment: "文件的大小",
  })
  file_size: number;

  @Column({
    type: "varchar",
    name: "file_type",
    length: 50,
    nullable: true,
    comment: "文件的MIME类型（如 image/jpeg, application/pdf 等）",
  })
  file_type: number;

  @Column({
    type: "tinyint",
    name: "is_active",
    nullable: true,
    comment:
      "表示文件是否处于活动状态（例如，是否被删除或标记为不可用）0删除 1可用",
  })
  is_active: number;

  @Column({
    type: "int",
    name: "uploader_id",
    nullable: true,
    comment: "上传文件的用户或实体的ID",
  })
  uploader_id: number;

  @Column({
    type: dbConfig.systemDB === "sqlite" ? "datetime" : "timestamp",
    default: () => "CURRENT_TIMESTAMP",
    comment: "当前时间戳",
  })
  created: Date;

  @Column({
    type: dbConfig.systemDB === "sqlite" ? "datetime" : "timestamp",
    default: () => "CURRENT_TIMESTAMP",
    comment: "修改时间戳",
  })
  updated: Date;

  @BeforeUpdate()
  updateTimestamp() {
    this.updated = new Date();
  }
}
